package oop.data;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import oop.db.dao.SectionDAO;
import oop.db.dao.TopicDAO;
import oop.db.dao.UserDAO;

public class Test implements Article {

	private long id;
	private String name;
	private String type;
	private String description;
	private long authorId;
	private Date createDate;
	private long topicId;
	private int time;
	private List<Section> sections = null;

	public Test(long id, String name, String description, Date createDate,
			long topicId, long authorId, String type, int time) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.createDate = createDate;
		this.topicId = topicId;
		this.authorId = authorId;
		this.type = type;
		this.time = time;
	}
	
	public List<Section> getSections() {
		if (sections == null) {
			try {
				sections = SectionDAO.fetchByTest(this);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return sections;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String descpription) {
		this.description = descpription;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public long getId() {
		return id;
	}

	public Topic getTopic() throws SQLException {
		return TopicDAO.fetchById(topicId);
	}
	
	public void setTopic(Topic topic) {
		this.topicId = topic.getId();
	}

	public User getAuthor() throws SQLException {
		return UserDAO.fetchById(authorId).get(0);
	}
	
	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public int getTime() {
		return time;
	}
	
	public int getQuestionCount() throws SQLException {
		int sum = 0;
		for (Section section : getSections()) {
			sum += section.getQuestions().size();
		}
		return sum;
	}

	@Override
	public Namespace getNamespace() {
		return Namespace.TEST;
	}

	@Override
	public String getQualifiedName() {
		return getNamespace().getName() + ":" + getName();
	}
	
}
